package com.stream.demo;

import org.junit.jupiter.api.Test;

public class Suanfa4 {
    @Test
    void gcd(){
//        System.out.println(gcds(8, 7));
        int[] a = {2, 3, 4, 5, 6, 7};
        System.out.println(rank(3, a));
//        String ww = "i am boy";
//        char[] chars = ww.toCharArray();
//        char[] w = {'s', 'a', 'd'};
//        reversw(chars);
//        String z = new String(chars);
//        System.out.println(Arrays.toString(chars));
//        System.out.println(z);
//        int i = BinarySearch(3, a);
//        System.out.println(i);
    }

    public static void main(String[] args) {

    }
    public static void reversw(char[] a){
        int N = a.length;
        for (int i = 0; i <N/2; i++) {
            char temp = a[i];
            a[i] = a[N-i-1];
            a[N-i-1] = temp;
        }
    }
    public static void revers(int[] a){
        int N = a.length;
        for (int i = 0; i <N/2; i++) {
            int temp = a[i];
            a[i] = a[N-1-i];
            a[N-1-i] = temp;
        }
    }

    public static int BinarySearch(int key, int[] a){
        int lo = 0;
        int hi = a.length-1;
        while (lo <= hi){
            int mid = lo + (hi-lo)/2;
            if(key<a[mid]) hi = mid-1;
            else if(key>a[mid]) lo = mid+1;
            else return mid;
        }
        return -1;
    }

    public static int gcds(int p, int q){
        if(q ==0) return p;
        int r = p%q;
        return  gcds(q,r);
    }
    public static int rank(int key,int[] a){
        return rank(key,a,0,a.length-1);
    }
    public static int rank(int key, int[] a, int lo, int hi){
        if(lo > hi) return -1;
        int mid = lo + (hi-lo)/2;
        if(key > a[mid])      return rank(key, a, mid+1, hi);
        else if(key < a[mid]) return rank(key, a, lo, mid-1);
        else                  return mid;
    }

}
